<?php
/*------------------------------------------------------ */
//-- 会员主页
//-- Author: iqgmy
/*------------------------------------------------------ */
namespace app\member\controller;
use app\ClientbaseController;
use app\mainadmin\model\SettingsModel;
use app\member\model\UsersModel;
use think\Db;

class Center  extends ClientbaseController{
  
	/*------------------------------------------------------ */
	//-- 首页
	/*------------------------------------------------------ */
	public function index(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']);
        $map['user_id'] =$this->userInfo['user_id'];
        $map['status'] = 1;
        $store = Db::name('distribution_store')->where($map)->value('store_id');
        $UsersModel = new UsersModel();
        $all=$UsersModel->userShareStats($this->userInfo['user_id']);
        $count['num_all']=$all['all'];
        $count['collect'] = Db::name('shop_goods_collect')->where(['user_id'=>$this->userInfo['user_id'],'status'=>1])->count();
        $count['comment'] = Db::name('shop_goods_comment')->where(['user_id'=>$this->userInfo['user_id'],'status'=>2])->count();
        //订单提醒
        $where[] = ['order_type','in',[0,1]];
        $where[] = ['user_id','=',$this->userInfo['user_id']];
        $where[] = ['is_split','=',0];
        $where[] = ['is_del','=',0];
        
        $where1[] = ['is_pay', '>', 0];
        $where1[] = ['order_status', '=', '0'];
        $where1[] = ['pay_status', '=', '0'];
        $order1 = Db::table('shop_order_info')->where($where)->where($where1)->count('order_id');
        $where2[] = ['order_status', '=', '1'];
        $where2[] = ['shipping_status', '=', '0'];
        // $where2['and'][] = "(pay_status = 1 OR is_pay = 0)";
        $where2[] = ['pay_status', '=','1'];
        $order2 = Db::table('shop_order_info')->where($where)->where($where2)->count('order_id');        
        $where3[] = ['order_status', '=', '1'];
        $where3[] = ['shipping_status', '=', '1'];
        $order3 = Db::table('shop_order_info')->where($where)->where($where3)->count('order_id');             
        $where4[] = ['order_status', '=', '1'];
        $where4[] = ['shipping_status', '=', '2'];
        $order4 = Db::table('shop_order_info')->where($where)->where($where4)->count('order_id');     
        $this->assign('order1', $order1);
        $this->assign('order2', $order2);
        $this->assign('order3', $order3);
        $this->assign('order4', $order4);
        
        //总收入
        $this->assign('count', $count);
        $this->assign('title', '会员中心');
        $this->assign('isUserIndex', 1);
		return $this->fetch('index');
	}
	/*------------------------------------------------------ */
    //-- 我的分享二维码
    /*------------------------------------------------------ */
    public function myCode(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 

		$DividendShareByRole = settings('DividendShareByRole');
		if ($DividendShareByRole == 1 && $this->userInfo['role_id'] < 1){
			return $this->error('请升级身份后再操作.');
		}
        $this->assign('title', '我的二维码');
        return $this->fetch('my_code');
    }
    /*------------------------------------------------------ */
    //-- 会员收货地址页
    /*------------------------------------------------------ */
    public function address(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 
        $this->assign('title', '收货地址');
        return $this->fetch('address/index');
    }
    /*------------------------------------------------------ */
    //-- 会员优惠券页
    /*------------------------------------------------------ */
    public function bonus(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 

        $this->assign('title', '优惠券');
        return $this->fetch('shop@bonus/index');
    }
    /*------------------------------------------------------ */
    //-- 公告详情
    /*------------------------------------------------------ */
    public function Details(){
        $setcolor = new SettingsModel();	
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']);
        $this->assign('title', '公告详情');
        return $this->fetch('Details');
    }
    /*------------------------------------------------------ */
    //-- 会员设置页
    /*------------------------------------------------------ */
    public function setting(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 

        $this->assign('title', '设置');
        return $this->fetch('setting');
    }
    /*------------------------------------------------------ */
    //-- 修改密码
    /*------------------------------------------------------ */
    public function editPwd(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 
        $this->assign('title', '修改密码');
        return $this->fetch('edit_pwd');
    }
    /*------------------------------------------------------ */
    //-- 修改支付密码
    /*------------------------------------------------------ */
    public function editPayPwd(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 

        $this->assign('sms_fun', settings('sms_fun'));//获取短信配置
        $this->assign('title', '修改支付密码');
        return $this->fetch('edit_pay_pwd');
    }
    /*------------------------------------------------------ */
    //-- 个人资料
    /*------------------------------------------------------ */
    public function userInfo(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 

        $this->assign('title', '个人资料');	
		$superior = (new UsersModel)->getSuperior($this->userInfo['pid']);
		$this->assign('superior', $superior);
        return $this->fetch('user_info');
    }
    /*------------------------------------------------------ */
    //-- 我的钱包
    /*------------------------------------------------------ */
    public function wallet(){
        $setcolor = new SettingsModel();	
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']);
        $this->assign('title', '我的钱包');
        return $this->fetch('wallet');
    }
    /*------------------------------------------------------ */
    //-- 提现
    /*------------------------------------------------------ */
    public function withdraw(){
        $setcolor = new SettingsModel();	
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']);
        $this->assign('title', '提现');
        return $this->fetch('withdraw');
    }
    /*------------------------------------------------------ */
    //-- 添加银行卡
    /*------------------------------------------------------ */
    public function addBankCard(){
        $this->assign('title', '添加银行卡');
        return $this->fetch('add_bank_card');
    }
    /*------------------------------------------------------ */
    //-- 申请代理商
    /*------------------------------------------------------ */
    public function user_region(){
        $user_region = Db::name('user_region')->where(['user_id'=>$this->userInfo['user_id']])->where('status','<>','2')->find();
        if($user_region){
            return $this->redirect('shop/index/index');
        }
        $data = Db::name('user_region')->where(['user_id'=>$this->userInfo['user_id']])->find();
        $province = Db::name('main_region')->where('level_type',1)->field('id,name')->select();
        $level_type= Db::name('main_region')->where('id',$data['region'])->find();
        if($data['region']){
            $level_type = Db::name('main_region')->where('id',$data['region'])->find();
            if($level_type['level_type'] == 1){
                $cityce= Db::name('main_region')->where('pid',$level_type['id'])->find();
                $district= Db::name('main_region')->where('pid',$cityce['id'])->find();
                $data['province_id'] = $level_type['id'];
                $data['city_id'] = $cityce['id'];
                $data['district_id'] = $district['id'];
            }elseif($level_type['level_type'] == 2){
                $province= Db::name('main_region')->where('id',$level_type['pid'])->find();
                $district= Db::name('main_region')->where('pid',$level_type['id'])->find();
                $data['province_id'] = $province['id'];
                $data['city_id'] = $level_type['id'];
                $data['district_id'] = $district['id'];
            }elseif($level_type['level_type'] == 3){
                $cityce= Db::name('main_region')->where('id',$level_type['pid'])->find();
                $province= Db::name('main_region')->where('id',$cityce['pid'])->find();
                $data['province_id'] = $province['id'];
                $data['city_id'] = $cityce['id'];
                $data['district_id'] = $level_type['id'];
            }

            $this->assign('level_type', $level_type);
        }
        $city = Db::name('main_region')->where('level_type',2)->field('id,name')->select();
        $district = Db::name('main_region')->where('level_type',3)->field('id,name')->select();
        $this->assign('city', $city);
        $this->assign('district', $district);
        $this->assign('title', '申请代理商');
        $this->assign('province', $province);
        $this->assign('level_type', $level_type['level_type']);
        $this->assign('province_id', $province);
        $this->assign('data', $data);
        $isbuy = 0;
        // 查询是否已经购买订单
        $allorder = Db::table('shop_order_info')->where('user_id',$this->userInfo['user_id'])->where('pay_status',1)->select();
        foreach($allorder as $orderv){
            $goods = Db::table('shop_goods')->where('goods_id',$orderv['buy_goods_id'])->where('is_type','<>',0)->find();
            if($goods){//购买代理产品
                $isbuy = 1;
                break;
            }
        }
        $this->assign('isbuy', $isbuy);
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 
        return $this->fetch('user_region');
        
    }

    /*------------------------------------------------------ */
    //-- 获取地区
    /*------------------------------------------------------ */
    public function getRegion(){
        $parent_id = input('get.parent_id/d');
        $selected = input('get.selected',0);
        $data = Db::name('main_region')->where("pid",$parent_id)->select();
        $html = '';
        if($data){
            foreach($data as $h){
                if($h['id'] == $selected){
                    $html .= "<option value='{$h['id']}' selected>{$h['name']}</option>";
                }
                $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
            }
        }
        echo $html;
    }

    /*------------------------------------------------------ */
    //-- 添加代理商
    /*------------------------------------------------------ */
    public function add_user_region(){
        $data = input('');
        if(!$data['name'] || !$data['phone']  || !$data['region_address']  || !$data['region'] || !$data['level']  ){
            $res['code'] = 0;
            $res['msg'] = '请填写完整.';
            return $this->ajaxReturn($res);
        }
        if (checkMobile($data['phone']) == false) {
            $res['code'] = 0;
            $res['msg'] = '手机号码不正确.';
            return $this->ajaxReturn($res);
        }
        
        $user_region = Db::name('user_region')->where(['user_id'=>$this->userInfo['user_id'],'status'=>0])->find();
        if($user_region){
            $res['code'] = 0;
            $res['msg'] = '您已成功提交申请，等待审核通过.';
            return $this->ajaxReturn($res);
        }


        switch ($data['level']) {
            case 1:
                $proxy = Db::name('user_region')->where('region',$data['region'])->where('status','in',[0,1])->where('level',1)->value('id');
                if($proxy){
                    $res['code'] = 0;
                    $res['msg'] = '该代理已存在.';
                    return $this->ajaxReturn($res);
                }
                $Arr['region'] = $data['region'];
                break;
            case 2:
                $proxy = Db::name('user_region')->where('region',$data['region'])->where('status','in',[0,1])->where('level',2)->value('id');
                if($proxy){
                    $res['code'] = 0;
                    $res['msg'] = '该代理已存在.';
                    return $this->ajaxReturn($res);
                }
                $Arr['region'] = $data['region'];
                # code...
                break;
            case 3:
                $proxy = Db::name('user_region')->where('region',$data['region'])->where('status','in',[0,1])->where('level',3)->value('id');
                if($proxy){
                    $res['code'] = 0;
                    $res['msg'] = '该代理已存在.';
                    return $this->ajaxReturn($res);
                }
                $Arr['region'] = $data['region'];
            # code...
                break;
            case 4:
                $proxy = Db::name('user_region')->where('region',$data['region'])->where('status','in',[0,1])->where('level',4)->value('id');
                if($proxy){
                    $res['code'] = 0;
                    $res['msg'] = '该代理已存在.';
                    return $this->ajaxReturn($res);
                }
                $Arr['region'] = $data['region'];
            # code...
                break;
            default:
                $res['code'] = 0;
                $res['msg'] = '无效申请.';
                return $this->ajaxReturn($res);
                # code...
                break;
        }

        $Arr['user_id'] = $this->userInfo['user_id'];
        $Arr['level']   = $data['level'];
        $Arr['order_id']= 0;
        $Arr['name']    = $data['name'];
        $Arr['phone']   = $data['phone'];
        $Arr['region_address'] = $data['region_address'];
        $Arr['region'] =  $Arr['region'];
        $Arr['status'] =  0;
        $Arr['add_time'] = time();
        $re = Db::name('user_region')->insertGetId($Arr);
        if($re){
            $res['code'] = 1;
            $res['msg'] = '申请提交成功.';
        }else{
            $res['code'] = 0;
            $res['msg'] = '服务器错误，请稍后再试.';
        }
        return $this->ajaxReturn($res);

    }
    public function isbuyuporder(){
        $map['user_id'] =$this->userInfo['user_id'];
        $user_store = Db::name('user_region')->where($map)->where('status','in',[0,1])->find();
        $allorder = Db::table('shop_order_info')->where('user_id',$this->userInfo['user_id'])->where('pay_status',1)->select();
        foreach($allorder as $orderv){
            $goods = Db::table('shop_goods')->where('goods_id',$orderv['buy_goods_id'])->where('is_type','<>',0)->find();
            if($goods){//购买代理产品
                if($user_store['level'] == $goods['is_type']){
                    Db::name('user_region')->where('id',$user_store['id'])->update(['order_id'=>$orderv['order_id']]);
                }
            }
        }
        $res['code'] = 1;
        $res['msg'] = '成功.';
        return $this->ajaxReturn($res);
    }

    /*------------------------------------------------------ */
    //-- 申请代理商
    /*------------------------------------------------------ */
    public function distribution_store(){
        $province = Db::name('main_region')->where('level_type',1)->field('id,name')->select();
        $data = Db::name('distribution_store')->where(['user_id'=>$this->userInfo['user_id']])->find();
        if($data){
//            $province_id = Db::name('main_region')->where(['id'=>$data['province_id']])->field('id,name')->find();
//            $city_id = Db::name('main_region')->where(['id'=>$data['city_id']])->field('id,name')->select();
//            $district_id = Db::name('main_region')->where(['id'=>$data['district_id']])->field('id,name')->select();
//            $province = Db::name('main_region')->where('level_type',1)->field('id,name')->select();
            $city = Db::name('main_region')->where('level_type',2)->field('id,name')->select();
            $district = Db::name('main_region')->where('level_type',3)->field('id,name')->select();
        }
        $this->assign('title', '申请门店');
//        $this->assign('province', $province);
        $this->assign('province', $province);
        $this->assign('city', $city);
        $this->assign('district', $district);
        $this->assign('data', $data);
        return $this->fetch('distribution_store');
    }
    /*------------------------------------------------------ */
    //-- 添加门店
    /*------------------------------------------------------ */
    public function add_distribution_store(){
        $data = input('');

        if($this->request->IsAjax()){
            if(!$data['store_name'] || !$data['username'] || !$data['store_phone'] || !$data['identity_card'] || !$data['province_id'] || !$data['city_id'] || !$data['district_id'] || !$data['store_address']){
                $res['code'] = 0;
                $res['msg'] = '请填写完整.';
                return $this->ajaxReturn($res);
            }
            if (empty($data['imgfile1']) == false)  $Arr['store_img'] = $this->upload_store($data['imgfile1']);
            if (empty($data['imgfile2']) == false)  $Arr['identity_img_one'] = $this->upload_store($data['imgfile2']);
            if (empty($data['imgfile3']) == false)  $Arr['identity_img_tow'] = $this->upload_store($data['imgfile3']);
            if (empty($data['imgfile4']) == false)  $Arr['business_license'] = $this->upload_store($data['imgfile4']);
            if (empty($data['imgfile5']) == false)  $Arr['store_logo_img'] = $this->upload_store($data['imgfile5']);

//            if(!$data['identity_img_one'] || !$data['identity_img_tow'] || !$data['business_license'] || !$data['store_img']){
//                $res['code'] = 0;
//                $res['msg'] = '图片上传不完整.';
//                return $this->ajaxReturn($res);
//            }
            $merger_name = Db::name('main_region')->where(['id'=>$data['district_id']])->value('merger_name');
            $address = $merger_name.$data['store_address'];
            $ak = '0gQ8UzbzQ7q5klDTgdNiwZcwNgit4UzD';
            $url ="http://api.map.baidu.com/geocoding/v3/?address=".$address."&output=json&ak=".$ak;
            $data2 = file_get_contents($url);
            $data2 = str_replace('renderOption&&renderOption(', '', $data2);
            $data2 = str_replace(')', '', $data2);
            $data2 = json_decode($data2,true);
            if (!empty($data2) && $data2['status'] == 0) {
                $Arr['lat'] = $data2['result']['location']['lat']; //纬度
                $Arr['lng'] = $data2['result']['location']['lng']; //经度
                //返回经纬度结果
            }else{
                $res['code'] = 0;
                $res['msg'] = '请输入正确的详细地址.';
                return $this->ajaxReturn($res);
            }
            $Arr['introduction'] = $data['introduction'];
            $Arr['store_name'] = $data['store_name'];
            $Arr['username'] = $data['username'];
            $Arr['store_phone'] = $data['store_phone'];
            $Arr['identity_card'] = $data['identity_card'];
            $Arr['province_id'] = $data['province_id'];
            $Arr['city_id'] = $data['city_id'];
            $Arr['district_id'] = $data['district_id'];
            $Arr['store_address'] = $data['store_address'];
            $Arr['status'] = 0;
            $store=Db::name('distribution_store')->where(['user_id' => $data['user_id']])->find();
            if($store){
                $map['user_id'] = $data['user_id'];
                $map['status'] = 0;
                $user_store = Db::name('distribution_store')->where($map)->find();
                if(!empty($user_store)) {
                    $res['code'] = 0;
                    $res['msg'] = '您已有待审核信息.';
                    return $this->ajaxReturn($res);
                }
                $Arr['update_time'] = time();
                $re = Db::name('distribution_store')->where(['user_id' => $data['user_id']])->update($Arr);
//                dump($res);exit;
                if($re !== false){
                    $res['code'] = 1;
                    $res['msg'] = '修改提交成功.';
                    return $this->ajaxReturn($res);
                }else{
                    $res['code'] = 0;
                    $res['msg'] = '修改提交失败.';
                    return $this->ajaxReturn($res);
                }

            }else{
                $store = Db::name('distribution_store')->where(['province_id'=>$data['province_id'], 'city_id'=>$data['city_id'], 'district_id'=>$data['district_id']])->find();
                if($store['user_id'] == $this->userInfo['user_id']){
                    $res['code'] = 0;
                    $res['msg'] = '提交失败,该区域已有门店.';
                    return $this->ajaxReturn($res);
                }
                $Arr['create_time'] = time();
                $Arr['user_id'] = $data['user_id'];
//            dump($Arr);exit;
                $re = Db::name('distribution_store')->insertGetId($Arr);
                if($re !== false){
                    $res['code'] = 1;
                    $res['msg'] = '提交成功.';
                    return $this->ajaxReturn($res);
                }else{
                    $res['code'] = 0;
                    $res['msg'] = '提交失败.';
                    return $this->ajaxReturn($res);
                }
            }

        }


    }

    //图片上传
    public function upload_store($imgfile){
        $file_path = config('config._upload_') . 'store/' . substr($this->userInfo['user_id'], -1) . '/';
        makeDir($file_path);
        $file_name = $file_path . random_str(12) . '.jpg';
        file_put_contents($file_name, base64_decode(str_replace('data:image/jpeg;base64,', '', $imgfile)));
        $store = trim($file_name, '.');
        return $store;
    }

    // 商家码
    public function store_code(){
        $user = Db::name('distribution_store')->where(['user_id'=>$this->userInfo['user_id'],'status'=>1])->find();
        if (!$user){
            return $this->error('抱歉，没有该权限.');
        }
        $this->assign('title', '商家收款码');
        return $this->fetch();
    }

//    // 门店支付
//    public function store_pay(){
//
//        $data = Db::name('distribution_store')->where(['status'=>1])->find();
//
//        $this->assign('user_id',$this->userInfo['user_id']);
//        return $this->fetch();
//    }
    //余额转赠
    public function givemoney(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 
		$this->assign('user_money',$this->userInfo['account']['balance_money']); 
        $this->assign('title', '余额转赠');
        return $this->fetch('givemoney');
    }
    //查询代理
    public function regionquery(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 
        $this->assign('title', '区域代理查询');
        return $this->fetch('regionquery');
    }
    // 查询区域津贴
    public function regionaward(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 
        $this->assign('title', '区域津贴查询');
        return $this->fetch('regionaward');
    }
    public function orderaward(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 
        $this->assign('title', '订单收入明细');
        return $this->fetch('orderaward');
    }
    public function awardlog(){
        $setcolor = new SettingsModel();
        $color = $setcolor->getRows();
		$this->assign('setcolor',$color['systemcolor']); 
        $this->assign('title', '收入明细');
        return $this->fetch('allaward');
    }
}?>